package com.cheung.leetcode.normal;

import java.util.ArrayList;
import java.util.List;

public class LC17 {

    public static List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits.isEmpty()) {
            return result;
        }
        backtracking(digits, result, "", 0);
        return result;
    }

    public static void backtracking(String digits, List<String> result, String path, int index) {
        if (path.length() == digits.length()) {
            result.add(path);
            return;
        }
        // 获取第index个数字
        String num = String.valueOf(digits.charAt(index));
        // 获取数字对应的字母
        String str = numToStr(num);
        // 搜索每一个字母
        for (int i = 0; i < str.length(); i++) {
            path += str.charAt(i);
            backtracking(digits, result, path, index + 1);
            path = path.substring(0, index);
        }

    }

    private static String numToStr(String b) {
        return switch (b) {
            case "2" -> "abc";
            case "3" -> "def";
            case "4" -> "ghi";
            case "5" -> "jkl";
            case "6" -> "mno";
            case "7" -> "pqrs";
            case "8" -> "tuv";
            default -> "wxyz";
        };
    }

    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
    }
}
